﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Southridge.Business;
using Southridge.View;
using Southridge.Data;
using System.Windows;

namespace Southridge.View
{


    public class DataContextSetterBehavior
    {


        public static object GetDataContext(DependencyObject obj)
        {
            return (object)obj.GetValue(DataContextProperty);
        }

        public static void SetDataContext(DependencyObject obj, object value)
        {
            obj.SetValue(DataContextProperty, value);
        }
        /// <summary>
        /// DataContext is an attached dependency property; the value assigned to it is forwarded it to the DataContext property of the element this property is set 
        /// </summary>
        public static readonly DependencyProperty DataContextProperty =
            DependencyProperty.RegisterAttached("DataContext", typeof(object), typeof(DataContextSetterBehavior), new UIPropertyMetadata(new PropertyChangedCallback(OnDataContextChanged)));

        /// <summary>
        /// This is the callback whenever the DataContextProperty is set. 
        /// </summary>
        /// <param name="d">is the instance object that the property is set on, this is where we will set the DataContext on</param>
        /// <param name="e">includes the NewValue being set, we will forward this to d's DataContext </param>
        static void OnDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(d))
            {
                // We are not in design mode, ignore the setter 
                return;
            }
            FrameworkElement fe = d as FrameworkElement;
            if (fe != null)
            {
                fe.DataContext = e.NewValue;
            }
            else
            {
                FrameworkContentElement fce = d as FrameworkContentElement;
                if (fce != null)
                    fce.DataContext = e.NewValue;
            }
        }
    }


    public class DesignTimeViewModel
    {
        protected static ListingCollection _searchResults;
        public static FilterMetadata FilterMetadata
        {
            get
            {
                return FilterMetadata.Current;
            }
        }

 
        public static ListingCollection SearchResults
        {
            get
            {
                if (_searchResults == null)
                {
                    _searchResults = new ListingCollection();

                    for (int x = 0; x < 10; x++)
                    {
                        _searchResults.Add(new Listing
                        {
                            City = "Bellevue", 
                            Address1 = "One Microsoft Way", 
                            State =  new State ( "WA", null ) ,  
                            ZipCode = "98052" ,
                            BathroomCount = 2.5,
                            BedroomCount = 4,
                            MLS = "abcd",
                            SquareFootage = 2000,
                            Description = "Description",
                            Year = 1984,
                            ClientRating = 4,
                            Price = 150000.0f 

                        });
                    }
                }
                return _searchResults;
            }
        }
 

        static DataGridColumnsViewModel _dataGridColumnsView = new DataGridColumnsViewModel();
        public static DataGridColumnsViewModel DataGridColumnsView
        {
            get
            {
                if (_dataGridColumnsView == null)
                    _dataGridColumnsView = new DataGridColumnsViewModel();
                return _dataGridColumnsView;
            }
        }

        static Listing _listing;
        public static Listing SingleListing
        {
            get
            {
                if (_listing == null)
                {
                    _listing = new Listing();                    
                    _listing.Address1 = "One Microsoft Way";
                    _listing.City = "Redmond";
                    _listing.State = new State() { ID = "WA", Description = "Washington" };  
                    _listing.BathroomCount = 3;
                    _listing.BedroomCount = 3;
                    _listing.HasPhotos = true;
                    _listing.MLS = "123456";
                    _listing.Title = "Beautiful 3 BR, 2 BT in Seattle";
                    _listing.Year = 1987;
                    _listing.SquareFootage = 2000;
                    _listing.Price = 450000;
                    _listing.NeighborhoodId = 1;
                    _listing.Description = "Charming two bedroom house. This is a description. It is a multiline, longer description for a house.";
                    
                }
                return _listing;
            }
        }

        public static List<Listing> MultipleListings
        {
            get            
            {
                List<Listing> list = new List<Listing> ();
                list.Add(SingleListing);
                list.Add(SingleListing);
                return list; 
            }
        } 
        public static Profile DesignTimeProfile
        {
            get
            {
                var p = new Profile()
                {
                    FirstName = "John",
                    LastName = "Doe",
                    Email = "a@b.com",
                    HomePhoneNo = "332-234-233",
                    MobilePhoneNo = "213-123-1234",
                    Preference = DesignPreference,
                    Address1 = "One Microsoft Way", 
                    City = "Redmond",
                    State = new State ( "WA", null ) 
                };
                return p; 
            } 
        }

#if SQLCE 
        public static Address DesignAddress
        {
            get
            {
                return new Address()
                {
                    Address1 = "123 Main" , 
                    Address2  = "Bldg 20" , City = "Bellevue" , 
                    State = "WA" , ZipCode = "1235" 
                }; 
            } 
        }
#endif 

        public static Preference DesignPreference 
        { 
            get 
            {
                return new Preference () 
                { 
                     
                        Acreage  = 8000 , 
                        County = DesignCounty,  
                        // Neighborhood  = DesignNeighborhood, 
                        State = DesignState                     
                }; 
            }   
        }        

        public static State DesignState
        {
            get
            {
                return new State() { ID = "WA", Description = "Washington" }; 
            }
        } 

         public static Neighborhood DesignNeighborhood
         { 
                get  { 

                    return new Neighborhood ()
                    {  Name = "Bellevue" , Id = 100 , 
                        IsNearCommunityCenter = true, 
                        IsNearParks = true , 
                        IsHighGrowth= true, 
                        IsNearCenterCity = true, 
                        IsNearShopping = true                           
                    }; 
                } 
         }

         public static County DesignCounty
         {
             get
             {
                 return new County() { Name = "King", State =  "WA"   }; 
             } 
         } 
    } 
 
}
